package edu.stanford.prpl.impl;

import java.util.Calendar;

import edu.stanford.prpl.api.BlobResource;
import edu.stanford.prpl.api.Group;
import edu.stanford.prpl.api.Identity;
import edu.stanford.prpl.api.PRPLObjectManager;
import edu.stanford.prpl.api.Resource;
import edu.stanford.prpl.api.PRPLObjectManager.ResourceManager;
import edu.stanford.prpl.common.PRPLConstants;

public class ResourceImpl extends PRPLObjectImpl implements Resource {

	public static final String MODIFIED_DATE_URI = PRPLConstants.PRPL_PREFIX + "modifiedDate";
	public static final String NAME_URI = PRPLConstants.PRPL_PREFIX + "name";
	public static final String TAG_URI = PRPLConstants.PRPL_PREFIX + "tag";
	public static final String OWNER_URI = PRPLConstants.PRPL_PREFIX + "owner";
	public static final String TYPE_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type";
	
	ResourceManager resourceManager;

	String resourceName;

	public ResourceImpl(String objectURI, PRPLObjectManager objManager) {
		super(objectURI, objManager);
		this.resourceManager = objManager.getResourceManager();
	}

	//@Override
	public void addMetaData(String propertyURI, Object... values) {
		resourceManager.addResourceMetaData(objectURI, propertyURI, values);
	}

	//@Override
	public void addTags(String... tags) {
		resourceManager.addResourceTags(objectURI, tags);
	}

	//@Override
	public void addType(String typeURI) {
		resourceManager.addResourceType(objectURI, typeURI);
	}

	//@Override
	public BlobResource asBlobResource() {
		if (resourceManager.isBlobResource(objectURI))
			return resourceManager.getBlobResource(objectURI);
		else
			return null;
	}

	//@Override
	public boolean canAccess(Group group) {
		return resourceManager.checkAccessGroupURI(objectURI, group.getURI());
	}

	//@Override
	public boolean canAccess(Identity identity) {
		return resourceManager.checkAccessIdentityURI(objectURI, identity.getURI());
	}

	//@Override
	public Calendar getLastModifiedDate() {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(resourceManager.getResourceMetaLastModifiedDate(objectURI));
		return cal;
	}

	//@Override
	public Object[] getMetaData(String propertyURI) {
		return resourceManager.getResourceMetaData(objectURI, propertyURI);
	}

	//@Override
	public String[] getMetaDataProperties() {
		return resourceManager.getResourceMetaDataProperties(objectURI);
	}

	//@Override
	public String getName() {
		if (this.resourceName == null)
			this.resourceName = resourceManager.getResourceName(objectURI);
		return this.resourceName;
	}

	//@Override
	public Identity getOwner() {
		return resourceManager.getResourceOwner(objectURI);
	}

	//@Override
	public Group[] getSharingGroups() {
		return resourceManager.getAllGroupsSharing(objectURI);
	}

	//@Override
	public Identity[] getSharingIdentities() {
		return resourceManager.getAllIdentitiesSharing(objectURI);
	}

	//@Override
	public String[] getTags() {
		return resourceManager.getResourceTags(objectURI);
	}

	//@Override
	public String[] getTypes() {
		return resourceManager.getResourceTypes(objectURI);
	}

	//@Override
	public String getURI() {
		return this.objectURI;
	}

	//@Override
	public void removeMetaData(String propertyURI, Object... values) {
		resourceManager.removeResourceMetaData(objectURI, propertyURI, values);
	}

	//@Override
	public void removeType(String typeURI) {
		resourceManager.removeResourceType(objectURI, typeURI);

	}

	//@Override
	public void setLastModifiedDate(Calendar date) {
		resourceManager.setResourceMetaLastModifiedDate(objectURI, date.getTimeInMillis());
	}

	//@Override
	public void setMetaData(String propertyURI, Object... values) {
		resourceManager.setResourceMetaData(objectURI, propertyURI, values);
	}

	//@Override
	public void setName(String name) {
		if (resourceManager.setResourceName(objectURI, name))
			this.resourceName = name;
	}

	//@Override
	public void shareWith(Group group) {
		resourceManager.shareResourceWithGroups(objectURI, group.getURI());
	}

	//@Override
	public void shareWith(Identity identity) {
		resourceManager.shareResourceWithIdentities(objectURI, identity.getURI());
	}

	//@Override
	public void unshareWith(Group group) {
		resourceManager.unshareResourceWithGroups(objectURI, group.getURI());
	}

	//@Override
	public void unshareWith(Identity identity) {
		resourceManager.shareResourceWithIdentities(objectURI, identity.getURI());
	}

	//@Override
	public boolean isOwn() {
		return resourceManager.isOwn(objectURI);
	}
}
